
<!DOCTYPE HTML>
<html lang="" >
    <head>
        <meta charset="UTF-8">
        <meta content="text/html; charset=utf-8" http-equiv="Content-Type">
        <title>session · GitBook</title>
        <meta http-equiv="X-UA-Compatible" content="IE=edge" />
        <meta name="description" content="">
        <meta name="generator" content="GitBook 3.2.3">
        
        
        
    
    <link rel="stylesheet" href="../../gitbook/style.css">

    
            
                
                <link rel="stylesheet" href="../../gitbook/gitbook-plugin-highlight/website.css">
                
            
                
                <link rel="stylesheet" href="../../gitbook/gitbook-plugin-search/search.css">
                
            
                
                <link rel="stylesheet" href="../../gitbook/gitbook-plugin-fontsettings/website.css">
                
            
        

    

    
        
    
        
    
        
    
        
    
        
    
        
    

        
    
    
    <meta name="HandheldFriendly" content="true"/>
    <meta name="viewport" content="width=device-width, initial-scale=1, user-scalable=no">
    <meta name="apple-mobile-web-app-capable" content="yes">
    <meta name="apple-mobile-web-app-status-bar-style" content="black">
    <link rel="apple-touch-icon-precomposed" sizes="152x152" href="../../gitbook/images/apple-touch-icon-precomposed-152.png">
    <link rel="shortcut icon" href="../../gitbook/images/favicon.ico" type="image/x-icon">

    
    <link rel="next" href="06_php画图.html" />
    
    
    <link rel="prev" href="04_cookie机制.html" />
    

    </head>
    <body>
        
<div class="book">
    <div class="book-summary">
        
            
<div id="book-search-input" role="search">
    <input type="text" placeholder="Type to search" />
</div>

            
                <nav role="navigation">
                


<ul class="summary">
    
    

    

    
        
        
    
        <li class="chapter " data-level="1.1" data-path="../../">
            
                <a href="../../">
            
                    
                    前言
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.2" data-path="../php基础/01_php介绍.html">
            
                <a href="../php基础/01_php介绍.html">
            
                    
                    php基础
            
                </a>
            

            
            <ul class="articles">
                
    
        <li class="chapter " data-level="1.2.1" data-path="../php基础/01_php介绍.html">
            
                <a href="../php基础/01_php介绍.html">
            
                    
                    php介绍
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.2.2" data-path="../php基础/02_PHP基础语法.html">
            
                <a href="../php基础/02_PHP基础语法.html">
            
                    
                    php基础语法
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.2.3" data-path="../php基础/03_php变量.html">
            
                <a href="../php基础/03_php变量.html">
            
                    
                    php变量
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.2.4" data-path="../php基础/04_php数据类型.html">
            
                <a href="../php基础/04_php数据类型.html">
            
                    
                    php数据类型
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.2.5" data-path="../php基础/05_php常量.html">
            
                <a href="../php基础/05_php常量.html">
            
                    
                    php常量
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.2.6" data-path="../php基础/06_php运算符.html">
            
                <a href="../php基础/06_php运算符.html">
            
                    
                    php运算符
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.2.7" data-path="../php基础/07_php流程控制.html">
            
                <a href="../php基础/07_php流程控制.html">
            
                    
                    php流程控制
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.2.8" data-path="../php基础/08_php数组.html">
            
                <a href="../php基础/08_php数组.html">
            
                    
                    php数组
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.2.9" data-path="../php基础/09_php函数.html">
            
                <a href="../php基础/09_php函数.html">
            
                    
                    php函数
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.2.10" data-path="../php基础/10_php面向对象.html">
            
                <a href="../php基础/10_php面向对象.html">
            
                    
                    php面向对象
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.2.11" data-path="../php基础/11_php命名空间.html">
            
                <a href="../php基础/11_php命名空间.html">
            
                    
                    php命名空间
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.2.12" data-path="../php基础/12_php日期和时间.html">
            
                <a href="../php基础/12_php日期和时间.html">
            
                    
                    php日期时间
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.2.13" data-path="../php基础/13_php表单.html">
            
                <a href="../php基础/13_php表单.html">
            
                    
                    php表单
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.2.14" data-path="../php基础/14_php包含文件.html">
            
                <a href="../php基础/14_php包含文件.html">
            
                    
                    php包含文件
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.2.15" data-path="../php基础/15_php常用函数.html">
            
                <a href="../php基础/15_php常用函数.html">
            
                    
                    php常用函数
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.2.16" data-path="16_php常用变量函数.md">
            
                <span>
            
                    
                    php常用变量函数
            
                </a>
            

            
        </li>
    

            </ul>
            
        </li>
    
        <li class="chapter " data-level="1.3" data-path="01_php错误与异常.html">
            
                <a href="01_php错误与异常.html">
            
                    
                    php错误与异常
            
                </a>
            

            
            <ul class="articles">
                
    
        <li class="chapter " data-level="1.3.1" data-path="02_PhpStorm本地断点调试.html">
            
                <a href="02_PhpStorm本地断点调试.html">
            
                    
                    PhpStorm本地断点调试
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.3.2" data-path="03_php文件操作.html">
            
                <a href="03_php文件操作.html">
            
                    
                    php文件操作
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.3.3" data-path="04_cookie机制.html">
            
                <a href="04_cookie机制.html">
            
                    
                    cookie机制
            
                </a>
            

            
        </li>
    
        <li class="chapter active" data-level="1.3.4" data-path="05_session.html">
            
                <a href="05_session.html">
            
                    
                    session
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.3.5" data-path="06_php画图.html">
            
                <a href="06_php画图.html">
            
                    
                    php画图
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.3.6" data-path="07_php加密机制.html">
            
                <a href="07_php加密机制.html">
            
                    
                    php加密机制
            
                </a>
            

            
        </li>
    

            </ul>
            
        </li>
    

    

    <li class="divider"></li>

    <li>
        <a href="https://www.gitbook.com" target="blank" class="gitbook-link">
            Published with GitBook
        </a>
    </li>
</ul>


                </nav>
            
        
    </div>

    <div class="book-body">
        
            <div class="body-inner">
                
                    

<div class="book-header" role="navigation">
    

    <!-- Title -->
    <h1>
        <i class="fa fa-circle-o-notch fa-spin"></i>
        <a href="../.." >session</a>
    </h1>
</div>




                    <div class="page-wrapper" tabindex="-1" role="main">
                        <div class="page-inner">
                            
<div id="book-search-results">
    <div class="search-noresults">
    
                                <section class="normal markdown-section">
                                
                                <h1 id="session">session</h1>
<p>PHP session &#x53D8;&#x91CF;&#x7528;&#x4E8E;&#x5B58;&#x50A8;&#x5173;&#x4E8E;&#x7528;&#x6237;&#x4F1A;&#x8BDD;&#xFF08;session&#xFF09;&#x7684;&#x4FE1;&#x606F;&#xFF0C;&#x6216;&#x8005;&#x66F4;&#x6539;&#x7528;&#x6237;&#x4F1A;&#x8BDD;&#xFF08;session&#xFF09;&#x7684;&#x8BBE;&#x7F6E;&#x3002;</p>
<p>Session &#x4EE3;&#x8868;&#x7740;&#x670D;&#x52A1;&#x5668;&#x548C;&#x5BA2;&#x6237;&#x7AEF;&#x4E00;&#x6B21;&#x4F1A;&#x8BDD;&#x7684;&#x8FC7;&#x7A0B;&#x3002;Session &#x5BF9;&#x8C61;&#x5B58;&#x50A8;&#x7279;&#x5B9A;&#x7528;&#x6237;&#x4F1A;&#x8BDD;&#x6240;&#x9700;&#x7684;&#x5C5E;&#x6027;&#x53CA;&#x914D;&#x7F6E;&#x4FE1;&#x606F;&#x3002;&#x8FD9;&#x6837;&#xFF0C;&#x5F53;&#x7528;&#x6237;&#x5728;&#x5E94;&#x7528;&#x7A0B;&#x5E8F;&#x7684; Web &#x9875;&#x4E4B;&#x95F4;&#x8DF3;&#x8F6C;&#x65F6;&#xFF0C;&#x5B58;&#x50A8;&#x5728; Session &#x5BF9;&#x8C61;&#x4E2D;&#x7684;&#x53D8;&#x91CF;&#x5C06;&#x4E0D;&#x4F1A;&#x4E22;&#x5931;&#xFF0C;&#x800C;&#x662F;&#x5728;&#x6574;&#x4E2A;&#x7528;&#x6237;&#x4F1A;&#x8BDD;&#x4E2D;&#x4E00;&#x76F4;&#x5B58;&#x5728;&#x4E0B;&#x53BB;&#x3002;&#x5F53;&#x5BA2;&#x6237;&#x7AEF;&#x5173;&#x95ED;&#x4F1A;&#x8BDD;&#xFF0C;&#x6216;&#x8005; Session &#x8D85;&#x65F6;&#x5931;&#x6548;&#x65F6;&#x4F1A;&#x8BDD;&#x7ED3;&#x675F;&#x3002;</p>
<h2 id="&#x4F7F;&#x7528;session">&#x4F7F;&#x7528;Session</h2>
<p>&#x5728;&#x60A8;&#x628A;&#x7528;&#x6237;&#x4FE1;&#x606F;&#x5B58;&#x50A8;&#x5230; PHP session &#x4E2D;&#x4E4B;&#x524D;&#xFF0C;&#x9996;&#x5148;&#x5FC5;&#x987B;&#x542F;&#x52A8;&#x4F1A;&#x8BDD;&#x3002;</p>
<pre><code class="lang-php"><span class="hljs-meta">&lt;?php</span> session_start(); <span class="hljs-meta">?&gt;</span>

&lt;html&gt;
&lt;body&gt;
</code></pre>
<p>&#x4E0A;&#x9762;&#x7684;&#x4EE3;&#x7801;&#x4F1A;&#x5411;&#x670D;&#x52A1;&#x5668;&#x6CE8;&#x518C;&#x7528;&#x6237;&#x7684;&#x4F1A;&#x8BDD;&#xFF0C;&#x4EE5;&#x4FBF;&#x60A8;&#x53EF;&#x4EE5;&#x5F00;&#x59CB;&#x4FDD;&#x5B58;&#x7528;&#x6237;&#x4FE1;&#x606F;&#xFF0C;&#x540C;&#x65F6;&#x4F1A;&#x4E3A;&#x7528;&#x6237;&#x4F1A;&#x8BDD;&#x5206;&#x914D;&#x4E00;&#x4E2A; UID&#x3002;</p>
<h3 id="session&#x539F;&#x7406;">Session&#x539F;&#x7406;</h3>
<p>&#x5F53;&#x7B2C;&#x4E00;&#x6B21;&#x8BBF;&#x95EE;&#x7F51;&#x7AD9;&#x65F6;&#xFF0C;Seesion_start()&#x51FD;&#x6570;&#x5C31;&#x4F1A;&#x521B;&#x5EFA;&#x4E00;&#x4E2A;&#x552F;&#x4E00;&#x7684;Session ID&#xFF0C;&#x5E76;&#x81EA;&#x52A8;&#x901A;&#x8FC7;HTTP&#x7684;&#x54CD;&#x5E94;&#x5934;&#xFF0C;&#x5C06;&#x8FD9;&#x4E2A;Session ID&#x4FDD;&#x5B58;&#x5230;&#x5BA2;&#x6237;&#x7AEF;Cookie&#x4E2D;&#x3002;&#x540C;&#x65F6;&#xFF0C;&#x4E5F;&#x5728;&#x670D;&#x52A1;&#x5668;&#x7AEF;&#x521B;&#x5EFA;&#x4E00;&#x4E2A;&#x4EE5;Session ID&#x547D;&#x540D;&#x7684;&#x6587;&#x4EF6;&#xFF0C;&#x7528;&#x4E8E;&#x4FDD;&#x5B58;&#x8FD9;&#x4E2A;&#x7528;&#x6237;&#x7684;&#x4F1A;&#x8BDD;&#x4FE1;&#x606F;&#x3002;&#x5F53;&#x540C;&#x4E00;&#x4E2A;&#x7528;&#x6237;&#x518D;&#x6B21;&#x8BBF;&#x95EE;&#x8FD9;&#x4E2A;&#x7F51;&#x7AD9;&#x65F6;&#xFF0C;&#x4E5F;&#x4F1A;&#x81EA;&#x52A8;&#x901A;&#x8FC7;HTTP&#x7684;&#x8BF7;&#x6C42;&#x5934;&#x5C06;Cookie&#x4E2D;&#x4FDD;&#x5B58;&#x7684;Seesion ID&#x518D;&#x643A;&#x5E26;&#x8FC7;&#x6765;&#xFF0C;&#x8FD9;&#x65F6;Session_start()&#x51FD;&#x6570;&#x5C31;&#x4E0D;&#x4F1A;&#x518D;&#x53BB;&#x5206;&#x914D;&#x4E00;&#x4E2A;&#x65B0;&#x7684;Session ID&#xFF0C;&#x800C;&#x662F;&#x5728;&#x670D;&#x52A1;&#x5668;&#x7684;&#x786C;&#x76D8;&#x4E2D;&#x53BB;&#x5BFB;&#x627E;&#x548C;&#x8FD9;&#x4E2A;Session ID&#x540C;&#x540D;&#x7684;Session&#x6587;&#x4EF6;&#xFF0C;&#x5C06;&#x8FD9;&#x4E4B;&#x524D;&#x4E3A;&#x8FD9;&#x4E2A;&#x7528;&#x6237;&#x4FDD;&#x5B58;&#x7684;&#x4F1A;&#x8BDD;&#x4FE1;&#x606F;&#x8BFB;&#x51FA;&#xFF0C;&#x5728;&#x5F53;&#x524D;&#x811A;&#x672C;&#x4E2D;&#x5E94;&#x7528;&#xFF0C;&#x8FBE;&#x5230;&#x8DDF;&#x8E2A;&#x8FD9;&#x4E2A;&#x7528;&#x6237;&#x7684;&#x76EE;&#x7684;&#x3002; Session&#x4EE5;&#x6570;&#x7EC4;&#x7684;&#x5F62;&#x5F0F;&#x4F7F;&#x7528;&#xFF0C;&#x5982;&#xFF1A;$_SESSION[&apos;session&#x540D;&apos;]</p>
<pre><code class="lang-php">client&#x2014;&#x2014;&#x2014;&gt;<span class="hljs-number">1.</span>request&#x2014;&#x2014;&#x2014;&#x2014;&#x2014;&#x2014;&#x2014;&#x2014;-&gt;server
                            <span class="hljs-number">2.</span> session_start();
   |&lt;&#x2014;&#x2014;&#x2014;&#x2014;<span class="hljs-number">-3.</span>reponse(SESSION_ID)&lt;&#x2014;&#x2014;&#x2013;|
   |&#x2014;&#x2014;&#x2014;&#x2014;-&gt;<span class="hljs-number">4.</span>request(SESSION_ID)&#x2014;&#x2014;&#x2014;&gt;|
                            <span class="hljs-number">5.</span> session_start();
   |&lt;&#x2014;&#x2014;&#x2014;&#x2014;<span class="hljs-number">-6.</span>reponse(SESSION_ID)&lt;&#x2014;&#x2014;&#x2014;|
   |&#x2014;&#x2014;&#x2014;&#x2014;-&gt;<span class="hljs-number">7.</span>request(SESSION_ID + logout)&#x2013;&gt;|
                            <span class="hljs-number">8.</span> session_destroy();
   |&lt;&#x2014;&#x2014;&#x2014;&#x2014;<span class="hljs-number">-9.</span>reponse(&#x5220;&#x9664;cookie&#x6587;&#x4EF6;)&lt;&#x2014;&#x2014;-|
</code></pre>
<h2 id="&#x5B58;&#x50A8;-session-&#x53D8;&#x91CF;">&#x5B58;&#x50A8; Session &#x53D8;&#x91CF;</h2>
<p>&#x5B58;&#x50A8;&#x548C;&#x53D6;&#x56DE; session &#x53D8;&#x91CF;&#x7684;&#x6B63;&#x786E;&#x65B9;&#x6CD5;&#x662F;&#x4F7F;&#x7528; PHP $_SESSION &#x53D8;&#x91CF;&#xFF1A;</p>
<pre><code class="lang-php"><span class="hljs-meta">&lt;?php</span> session_start() <span class="hljs-meta">?&gt;</span>

$_SESSION[<span class="hljs-string">&apos;username&apos;</span>]=<span class="hljs-string">&apos;king&apos;</span>;
</code></pre>
<p>&#x6CE8;&#x610F;&#xFF1A;session_start()&#x51FD;&#x6570;&#x4E4B;&#x524D;&#x4E0D;&#x80FD;&#x6709;&#x4EFB;&#x4F55;&#x8F93;&#x51FA;</p>
<h2 id="&#x9500;&#x6BC1;-session">&#x9500;&#x6BC1; Session</h2>
<pre><code class="lang-php"><span class="hljs-meta">&lt;?php</span>
    session_start();  <span class="hljs-comment">//&#x542F;&#x52A8;&#x4F1A;&#x8BDD;</span>

    <span class="hljs-keyword">isset</span>($_SESSION[<span class="hljs-string">&apos;username&apos;</span>]); <span class="hljs-comment">//&#x68C0;&#x6D4B;&#x4E00;&#x4E2A;&#x53D8;&#x91CF;&#x662F;&#x5426;&#x6709;&#x503C;</span>

    <span class="hljs-keyword">unset</span>($_SESSION[<span class="hljs-string">&apos;username&apos;</span>]); <span class="hljs-comment">//&#x9500;&#x6BC1;&#x53D8;&#x91CF; username</span>
</code></pre>
<p>&#x53EF;&#x4EE5;&#x901A;&#x8FC7;session_destroy()&#x51FD;&#x6570;&#x5728;&#x9875;&#x9762;&#x4E2D;&#x63D0;&#x4F9B;&#x4E00;&#x4E2A;&#x201C;&#x9000;&#x51FA;&#x201D;&#x6309;&#x94AE;&#xFF0C;&#x901A;&#x8FC7;&#x5355;&#x51FB;&#x9500;&#x6BC1;&#x672C;&#x6B21;&#x4F1A;&#x8BDD;&#x3002;</p>
<h2 id="session&#x751F;&#x547D;&#x5468;&#x671F;">Session&#x751F;&#x547D;&#x5468;&#x671F;</h2>
<p>&#x6BCF;&#x6B21;&#x8D85;&#x8FC7;&#x4E86;SESSION&#x7684;&#x751F;&#x5B58;&#x5468;&#x671F;(session.gc_maxlifetime,&#x9ED8;&#x8BA4;&#x662F;1440&#x79D2;&#x4E5F;&#x5C31;&#x662F;24&#x5206;&#x949F;)&#x53BB;&#x8BBF;&#x95EE;&#x7684;&#x8BDD;,SESSION&#x4E00;&#x5B9A;&#x4F1A;&#x88AB;&#x56DE;&#x6536;.</p>

                                
                                </section>
                            
    </div>
    <div class="search-results">
        <div class="has-results">
            
            <h1 class="search-results-title"><span class='search-results-count'></span> results matching "<span class='search-query'></span>"</h1>
            <ul class="search-results-list"></ul>
            
        </div>
        <div class="no-results">
            
            <h1 class="search-results-title">No results matching "<span class='search-query'></span>"</h1>
            
        </div>
    </div>
</div>

                        </div>
                    </div>
                
            </div>

            
                
                <a href="04_cookie机制.html" class="navigation navigation-prev " aria-label="Previous page: cookie机制">
                    <i class="fa fa-angle-left"></i>
                </a>
                
                
                <a href="06_php画图.html" class="navigation navigation-next " aria-label="Next page: php画图">
                    <i class="fa fa-angle-right"></i>
                </a>
                
            
        
    </div>

    <script>
        var gitbook = gitbook || [];
        gitbook.push(function() {
            gitbook.page.hasChanged({"page":{"title":"session","level":"1.3.4","depth":2,"next":{"title":"php画图","level":"1.3.5","depth":2,"path":"php/php高级/06_php画图.md","ref":"./php/php高级/06_php画图.md","articles":[]},"previous":{"title":"cookie机制","level":"1.3.3","depth":2,"path":"php/php高级/04_cookie机制.md","ref":"./php/php高级/04_cookie机制.md","articles":[]},"dir":"ltr"},"config":{"gitbook":"*","theme":"default","variables":{},"plugins":["livereload"],"pluginsConfig":{"livereload":{},"highlight":{},"search":{},"lunr":{"maxIndexSize":1000000,"ignoreSpecialCharacters":false},"sharing":{"facebook":true,"twitter":true,"google":false,"weibo":false,"instapaper":false,"vk":false,"all":["facebook","google","twitter","weibo","instapaper"]},"fontsettings":{"theme":"white","family":"sans","size":2},"theme-default":{"styles":{"website":"styles/website.css","pdf":"styles/pdf.css","epub":"styles/epub.css","mobi":"styles/mobi.css","ebook":"styles/ebook.css","print":"styles/print.css"},"showLevel":false}},"structure":{"langs":"LANGS.md","readme":"README.md","glossary":"GLOSSARY.md","summary":"SUMMARY.md"},"pdf":{"pageNumbers":true,"fontSize":12,"fontFamily":"Arial","paperSize":"a4","chapterMark":"pagebreak","pageBreaksBefore":"/","margin":{"right":62,"left":62,"top":56,"bottom":56}},"styles":{"website":"styles/website.css","pdf":"styles/pdf.css","epub":"styles/epub.css","mobi":"styles/mobi.css","ebook":"styles/ebook.css","print":"styles/print.css"}},"file":{"path":"php/php高级/05_session.md","mtime":"2020-04-23T10:28:57.444Z","type":"markdown"},"gitbook":{"version":"3.2.3","time":"2020-04-23T11:06:38.883Z"},"basePath":"../..","book":{"language":""}});
        });
    </script>
</div>

        
    <script src="../../gitbook/gitbook.js"></script>
    <script src="../../gitbook/theme.js"></script>
    
        
        <script src="../../gitbook/gitbook-plugin-livereload/plugin.js"></script>
        
    
        
        <script src="../../gitbook/gitbook-plugin-search/search-engine.js"></script>
        
    
        
        <script src="../../gitbook/gitbook-plugin-search/search.js"></script>
        
    
        
        <script src="../../gitbook/gitbook-plugin-lunr/lunr.min.js"></script>
        
    
        
        <script src="../../gitbook/gitbook-plugin-lunr/search-lunr.js"></script>
        
    
        
        <script src="../../gitbook/gitbook-plugin-sharing/buttons.js"></script>
        
    
        
        <script src="../../gitbook/gitbook-plugin-fontsettings/fontsettings.js"></script>
        
    

    </body>
</html>

